package com.moyu.myadmin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moyu.myadmin.model.vo.*;
import com.moyu.myadmin.service.ProcessService;
import com.moyu.myadmin.utils.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author jiaomingyu5778@gmail.com
 * @date 2022/2/28 14:02
 */
@Validated
@Log4j2
@Api(tags = {"流程管理接口"})
@RestController
@RequestMapping("/process/")
public class ProcessController {

    private final ProcessService service;

    public ProcessController(ProcessService service) {
        this.service = service;
    }


    @ApiOperation(value = "分页查询流程代办信息")
    @PostMapping("queryListPage")
    public ResultData<Page<TaskVO>> queryList() {
        log.info("分页查询流程代办信息，查询信息：");
        Page<TaskVO> page = service.queryListPage();
        return ResultData.success(page);
    }

    @ApiOperation(value = "通过processDefinitionId得到已部署的流程模型的字节流，如BPMN 2.0 xml文件")
    @GetMapping("getProcessModel")
    public ResultData<String> getProcessModel(@NotEmpty(message = "流程定义 ID 不能为空") String processDefinitionId) {
        log.info("通过processDefinitionId得到已部署的流程模型的字节流，如BPMN 2.0 xml文件，processDefinitionId：{}", processDefinitionId);
        String processModel = service.getProcessModel(processDefinitionId);
        return ResultData.success(processModel);
    }

    @ApiOperation(value = "获取流程审批记录")
    @GetMapping("getTaskComment")
    public ResultData<List<TaskCommentVO>> getTaskComment(@NotEmpty(message = "流程实例 ID 不能为空") String processInstanceId) {
        log.info("获取流程审批记录：{}", processInstanceId);
        List<TaskCommentVO> taskComment = service.getTaskComment(processInstanceId);
        return ResultData.success(taskComment);
    }

    @ApiOperation(value = "获取流程高亮节点")
    @GetMapping("getHighLightedFlows")
    public ResultData<ActivityHighLineVO> getHighLightedFlows(@NotEmpty(message = "流程实例 ID 不能为空") String processInstanceId) {
        log.info("获取流程高亮节点：{}", processInstanceId);
        ActivityHighLineVO lightedFlows = service.getHighLightedFlows(processInstanceId);
        return ResultData.success(lightedFlows);
    }

    @ApiOperation(value = "获取流程相关信息")
    @GetMapping("getProcessInfo")
    public ResultData<ProcessInfoVO> getProcessInfo(@NotEmpty(message = "流程实例 ID 不能为空") String processInstanceId,
                                                    @NotEmpty(message = "流程定义 ID 不能为空") String processDefinitionId) {
        log.info("获取流程相关信息：{}", processInstanceId);
        ProcessInfoVO processInfo = service.getProcessInfo(processInstanceId, processDefinitionId);
        return ResultData.success(processInfo);
    }

    @ApiOperation(value = "审批通过")
    @PostMapping("completeTask")
    public ResultData<String> completeTask(@Valid @RequestBody CompleteTaskVO completeTask) {
        log.info("审批通过：{}", completeTask);
        service.completeTask(completeTask);
        return ResultData.success("审批通过");
    }

    @ApiOperation(value = "查询历史任务")
    @PostMapping("queryHistoryProcess")
    public ResultData<Page<HistoricProcessVO>> queryHistoryTask(@Valid @RequestBody HistoricProcessVO historicProcess) {
        log.info("查询历史任务：{}", historicProcess);
        Page<HistoricProcessVO> page = service.queryHistoryProcess(historicProcess);
        return ResultData.success(page);
    }

    @ApiOperation(value = "流程退回")
    @PostMapping("backProcess")
    public ResultData<String> backProcess(@Valid @RequestBody BackProcessVO backProcess) {
        log.info("流程退回：{}", backProcess);
        service.backProcess(backProcess);
        return ResultData.success("流程退回");
    }
}
